﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ProductManagement;

public partial class Modules_Purchase_PurchaseRequest : System.Web.UI.UserControl
{
    Products _products = new Products();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Session["Purchase_toSearch"] = "";
            //Session["Purchase_SearchCriterea"] = "materials";
            //ViewMaterials();
            FillDDLProducts();
        }
    }
    

    protected void ddlProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlProduct.SelectedIndex != 0)
        {
            Session["Purchase_SearchCriterea"] = "product";
            Session["Purchase_toSearch"] = ddlProduct.SelectedValue;
            ViewMaterials();
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["Purchase_toSearch"] = txtSearch.Text;
        Session["Purchase_SearchCriterea"] = "materials";
        ViewMaterials();
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Session["Purchase_btnClicked"]="btnSave";
        if (CheckSelection() != "None Selected")
        {
        }
    }
    //protected void btnViewStatus_Click(object sender, EventArgs e)
    //{
    //    Session["Purchase_btnClicked"] = "btnViewStatus";
    //    if (CheckSelection() == "Single Selected")
    //    { }
    //} 


    protected void gvwPurchaseRequest_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //ShowGridColumns();
        gvwPurchaseRequest.EditIndex = e.NewEditIndex;
    }
    protected void gvwPurchaseRequest_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        var row = gvwPurchaseRequest.Rows[e.RowIndex];       

       //TextBox txtQuantity=(TextBox)(row.Cells[3].Controls[1]);      
       //TextBox txtPriority = (TextBox)(row.Cells[4].Controls[0]);
       TextBox txtQuantity = (TextBox)(row.Cells[4].Controls[0]);
       string materialID = row.Cells[1].Text;
       string productID = ddlProduct.SelectedValue;
       //request.MaterialID = row.Cells[1].Text;
       //request.RequestQuantity= txtQuantity.Text;
       //request.RequestDate = DateTime.Now.ToShortDateString();
       //request.Priority  = txtPriority.Text;
       //request.RequesterID = "";
       //request.RequesterRemarks = txtRemarks.Text;

       if (txtQuantity.Text == string.Empty)
       {
           Response.Write("<script>alert('Enter Quantity')</script>");
           return;
       }
             
      // _purchase.SubmitRequest(request);
    //   _products.EditBOM(productID, materialID, txtQuantity.Text);
       gvwPurchaseRequest.EditIndex = -1;
       ViewMaterials();
       //HideGridColumns();
    }
    protected void gvwPurchaseRequest_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvwPurchaseRequest.EditIndex = -1;
        //HideGridColumns();
    }

    void FillDDLProducts()
    {
        DataSet dsCategory = _products.GetData("ProductDetails", "");
        ddlProduct.DataSource = dsCategory;
        ddlProduct.DataTextField = "ProductName";
        ddlProduct.DataValueField = "ProductID";
        ddlProduct.DataBind();
        ddlProduct.Items.Insert(0,"Select");

    }

    void ViewMaterials()
    {
        DataSet dsMaterials = new DataSet();

//        _products.SearchMaterials(Session["Purchase_SearchCriterea"].ToString(), Session["Purchase_toSearch"].ToString(), out dsMaterials);


        gvwPurchaseRequest.DataSource = dsMaterials;
        //ShowGridColumns();
        gvwPurchaseRequest.Columns[1].Visible = true;
        gvwPurchaseRequest.DataBind();
        //HideGridColumns();
        gvwPurchaseRequest.Columns[1].Visible = false;
        Session["Purchase_dataset"] = dsMaterials;
    }

    string CheckSelection()
    {
        string status = string.Empty;
        try
        {
            int count = 0;
            string[] ID = new string[gvwPurchaseRequest.Rows.Count];

            for (int i = 0; i < gvwPurchaseRequest.Rows.Count; i++)
            {
                CheckBox chkGrid = (CheckBox)(gvwPurchaseRequest.Rows[i].Cells[0].FindControl("chkSelect"));
                if (chkGrid.Checked == true)
                {
                    ID[count] = gvwPurchaseRequest.Rows[i].Cells[1].Text;
                    count++;
                }
            }

            if (count == 0)
            {
                Response.Write("<script>alert('Select an Item')</script>");
                status = "None Selected";
                return status;
            }
            else if (count > 1)
            {
                Response.Write("<script>alert('Select Single Item')</script>");
                status = "Multiple Selected";
            }
            else
            {
                status = "Single Selected";
            }

            Session["UserManager_IDs"] = ID;
            Session["UserManager_selectedUsers"] = count;
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Check Selection Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
        return status;

    }
    //void ShowGridColumns()
    //{
    //    gvwPurchaseRequest.Columns[3].Visible = true;
    //    gvwPurchaseRequest.Columns[4].Visible = true;
    //    gvwPurchaseRequest.Columns[5].Visible = true;
    //}

    //void HideGridColumns()
    //{
    //    gvwPurchaseRequest.Columns[3].Visible = false;
    //    gvwPurchaseRequest.Columns[4].Visible = false;
    //    gvwPurchaseRequest.Columns[5].Visible = false;
    //}



    protected void gvwPurchaseRequest_RowDataBound(object sender, GridViewRowEventArgs e)
    {


        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataSet dsVendor = new DataSet();
            dsVendor = _products.GetData("VendorDetails", "");
            DropDownList ddlGrid = (DropDownList)e.Row.FindControl("ddlVendor");
            ddlGrid.DataSource = dsVendor;
            ddlGrid.DataValueField = "VendorID";
            ddlGrid.DataTextField = "VendorName";
            ddlGrid.DataBind();
            ddlGrid.Items.Insert(0, "Select");
        }

        //DataSet dsCategory = _purchase.GetData("VendorDetails", "");
        //ddlProduct.DataSource = dsCategory;
        //ddlProduct.DataTextField = "VendorName";
        //ddlProduct.DataValueField = "VendorID";
        //ddlProduct.DataBind();
        //ddlProduct.Items.Insert(0, "Select");

    }
}
